<template>
  <div>
    <el-dialog
      title="文章历史"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      class="mt-dialog"
      width="80%"
      @close="close"
    >

      <el-table
        v-loading.fullscreen.lock="listLoading"
        :data="data"
        border
        fit
        highlight-current-row
        style="width: 100%"
        @sort-change="handleSort"
      >

        <el-table-column align="center" prop="version_name" label="版本" sortable="custom" />

        <el-table-column align="center" label="标题" prop="title" sortable="custom">
          <!--<template slot-scope="scope">-->
          <!--{{ scope.row.title+"|"+scope.row.version_name }}-->
          <!--</template>-->
        </el-table-column>

        <el-table-column align="center" label="词数" prop="word_count" sortable="word_count" width="80px" />

        <el-table-column align="center" label="创建时间" prop="create_at" sortable="create_at" width="180px" />

        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="success" @click="view(scope.row.id)">查看</el-button>
            <el-button size="mini" type="danger" @click="apply(scope.row.id)">恢复至该版本</el-button>
          </template>
        </el-table-column>

      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { GetHistory as Get } from '@/api/cms/post'

import qs from 'qs'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'PostHistory',
  props: ['historyVisible', 'post_id'],
  data() {
    return {
      data: [],
      listLoading: true,
      order_by: '',
      order_desc: '',

      dialogVisible: false
    }
  },
  watch: {
    historyVisible(v) {
      this.dialogVisible = v
      if (v) {
        this.getList()
      }
    }
  },
  methods: {
    getList() {
      this.listLoading = true
      Get(null, qs.stringify({
        post_id: this.post_id,
        order_by: this.order_by,
        order_desc: this.order_desc
      }))
        .then(response => {
          this.scrollTop()
          this.data = response.data.data
          this.listLoading = false
        }).catch(e => {
          this.listLoading = false
        })
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    },
    close() {
      this.$emit('closeHistoryDialog')
    },
    view(v) {
      this.listLoading = true
      Get(v)
        .then(response => {
          this.listLoading = false
          this.$alert(response.data.content, response.data.title, {
            customClass: 'mt-message',
            dangerouslyUseHTMLString: true
          })
        })
        .catch(e => {
          this.listLoading = false
        })
    },
    apply(v) {
      this.listLoading = true
      Get(v)
        .then(response => {
          this.listLoading = false
          this.$emit('applyHistory', response.data)
        })
        .catch(e => {
          this.listLoading = false
        })
    }
  }
}
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .top >>> .el-select .el-input {
    width: 350px;
  }

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .top {
    padding-left: 20px;
    margin-bottom: 30px;
    margin-top: 28px;
  }

  .input-with-select {
    width: calc(100% - 200px);
  }

  .add-btn {
    float: right;
    margin-right: 20px;
    width: 150px
  }

  .mt-pagination {
    float: right;
    margin-top: 5px;
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 68px;
  }

  .mt-dialog >>> .el-transfer-panel {
    width: calc(50% - 60px);
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin-top: 60px;
    /*margin: 168px auto !important;*/
  }

  .mt-dialog >>> .el-dialog__body {
    padding-bottom: 100px;
  }
</style>
